package com.two.screen.water.balance.serial.weight

import com.two.screen.water.balance.listener.MyObserver
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.RxUtils
import com.two.screen.water.balance.utils.ToastUtil
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import java.text.DecimalFormat

object WeightAll {

    fun initSerialPortWeight() {
        Observable.create(ObservableOnSubscribe<String> { e ->
            try {
                weightClose()
                SerialPortWeightZzUtils.initSerialPortWeight()
            } catch (exc: Exception) {
                d("初始称重失败：$exc")
                e.onError(exc)
            }
        })
            .compose(RxUtils.nThread())
            .subscribe(object : MyObserver<String>() {
                override fun onNext(t: String) {
                    ToastUtil.showToast(t)
                }

                override fun onError(e: Throwable) {
                    ToastUtil.showToast(e.message.toString())
                }
            })
    }

    fun weightClose() {
        try {
            SerialPortWeightZzUtils.close()
        } catch (e: Exception) {
            d("关闭称重失败：$e")
        }
    }

    //置零
    fun setZero() {
        try {
            Observable.create(ObservableOnSubscribe<String> { e ->
                try {
                    SerialPortWeightZzUtils.setZero()
                    e.onNext("置零成功")
                } catch (ex: Exception) {
                    d("置零失败：$e")
                    e.onError(ex)
                }
            })
                .compose(RxUtils.nThread())
                .subscribe(object : MyObserver<String>() {
                    override fun onNext(t: String) {
                    }
                })

        } catch (e: Exception) {
            ToastUtil.showToast("置零失败：$e")

        }
    }

    //去皮
    fun setPeel() {
        SerialPortWeightZzUtils.setPeel()
    }

    //清皮
    fun clearPeel() {
        SerialPortWeightZzUtils.clearPeel()
    }

    fun d(msg: String) {
        CommUtils.println("WeightAll->", msg)
    }
}